package top.wardyan.common.interceptor;
import java.util.Map;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import top.wardyan.base.common.util.DateUtil;
public class RequestParamInterceptor implements Interceptor{

	private static final cn.hutool.log.Log log = cn.hutool.log.Log.get();

	@Override
	public void intercept(Invocation inv) {
	    Controller controller = inv.getController();
	    Map<String, String[]> params = controller.getParaMap();

	    // Console.log("--------------------------------------------------------------------------------");
	    Console.log("--------------------CTRL REPORT-------------------------------------------------");
	    Console.log(String.format("    %-15s %s %s", "timestamp", ":", DateUtil.formatNow()));
	    Console.log(String.format("    %-15s %s %s", "request_method", ":", inv.getController().getRequest().getMethod()));
	    Console.log(String.format("    %-15s %s %s", "action_method", ":", inv.getActionKey()));
	    Console.log("    params:");
	    for (Map.Entry<String, String[]> entry : params.entrySet()) {
	        String key = entry.getKey();
	        String[] values = entry.getValue();
	        StringBuilder valuesStr = new StringBuilder();
	        for (String value : values) {
	            valuesStr.append(value).append(" ");
	        }
	        Console.log(String.format("    %-18s %-15s %s %s", "", key, ":", valuesStr.toString().trim()));
	    }
	    Console.log("--------------------------------------------------------------------------------");
	    Console.log("");
	    inv.invoke();
	}
//
//    private String formatLogEntry(String label, String value) {
//        int maxLabelWidth = 15; // 设置最大标签宽度
//        int labelWidth = StrUtil.length(label);
//        int padding = maxLabelWidth - labelWidth;
//        if (padding > 0) {
//            return label + " ".repeat(padding) + value;
//        } else {
//            return label + value;
//        }
//    }
}
